package com.gitee.wsl.doc.prosemirror.plugin.writer

import com.gitee.wsl.doc.prosemirror.model.mark.Mark
import com.gitee.wsl.doc.prosemirror.model.node.Node


/**
 * 标记序列化规范接口
 */
sealed interface MarkSerializerSpec {
    val open: Any
    val close: Any
    val mixable: Boolean?
    val expelEnclosingWhitespace: Boolean?
    val escape: Boolean?

    companion object{
        val blankMark = BasicMarkSerializerSpec("", "")
    }
}

fun MarkSerializerSpec(openFunc: MarkSerializerFun,
                       closeFunc: MarkSerializerFun,
                       mixable: Boolean? = null,
                       expelEnclosingWhitespace: Boolean? = null,
                       escape: Boolean? = null
) = FunctionalMarkSerializerSpec(openFunc,closeFunc,mixable,expelEnclosingWhitespace,escape)

fun MarkSerializerSpec(open: String,
                       close: String,
                       mixable: Boolean? = null,
                       expelEnclosingWhitespace: Boolean? = null,
                       escape: Boolean? = null
) = BasicMarkSerializerSpec(open,close,mixable,expelEnclosingWhitespace,escape)

/**
 * 基础标记序列化规范实现
 */
data class BasicMarkSerializerSpec(
    override val open: String,
    override val close: String,
    override val mixable: Boolean? = null,
    override val expelEnclosingWhitespace: Boolean? = null,
    override val escape: Boolean? = null
) : MarkSerializerSpec

typealias MarkSerializerFun = (state: MarkdownSerializerState, mark: Mark, parent: Node, index: Int) -> String

/**
 * 函数标记序列化规范
 */
data class FunctionalMarkSerializerSpec(
    val openFunc: MarkSerializerFun,
    val closeFunc: MarkSerializerFun,
    override val mixable: Boolean? = null,
    override val expelEnclosingWhitespace: Boolean? = null,
    override val escape: Boolean? = null
) : MarkSerializerSpec {
    override val open: Any get() = openFunc
    override val close: Any get() = closeFunc
}





